<?php

$auth = Auth::getAuth('current_user');
$db = new Database();
$updateStatus = new UpdateStatus();
$id = $_POST['id'];
$status = $_POST['status'];
if (isset($_POST['type'])) {
    switch ($_POST['type']) {
        case 'orgchart':
            $updateStatus->deactivateOrgchart();
            if ($status == "0") { //orgchart activation
                //deactivate old orgchart
                $updateStatus->deactivateOrgchart();

                //activate orgchart
                $updateStatus->activateOrgchart($id);
            } else { //orgchart deactivation
                $updateStatus->deactivateOrgchart();
            }
            break;
        case 'form':
            if ($status == "0") { //form activation
                $updateStatus->activateForm($id);
            } else { //form deactivation
                $updateStatus->deactivateForm($id);
            }
            break;
        case 'workflow':

            if ($status == "0") { //workflow activation
                //deactivate old workflow
                $updateStatus->deactivateWorkflow($id);
                //activate new workflow
                $updateStatus->activateWorkflow($id);
            } else { //workflow deactivation
                $updateStatus->deactivateWorkflow($id);
            }

            break;
        case "report":
            if ($status == "0") {
                $reportDoc = new Report($db, $id);
                $reportDoc->is_active = 1;
                $reportDoc->update();
            } else {
                $reportDoc = new Report($db, $id);
                $reportDoc->is_active = 0;
                $reportDoc->update();
            }
        case 'delete_form' :
            $form = new Form($db, $id);
            // $db->query("DROP TABLE " . $form->form_table_name . "");
            $db->delete("tb_workspace", array("id" => $id));

            $getWorkflows = $db->query("SELECT * FROM tbworkflow WHERE form_id = $id", "array");
            foreach ($getWorkflows as $value) {
                $db->delete("tbworkflow_objects", array("workflow_id" => $value['id']));
                $db->delete("tbworkflow_lines", array("workflow_id" => $value['id']));
            }
            $db->delete("tbworkflow", array("id" => $id));
            break;
        case "delete_orgchart" :
            $db->delete("tborgchart", array("id" => $id));
            $db->delete("tborgchartobjects", array("orgChart_id" => $id));
            $db->delete("tborgchartline", array("orgChart_id" => $id));
            break;
        case "delete_workflow" :
            $db->delete("tbworkflow", array("id" => $id));
            $db->delete("tbworkflow_objects", array("workflow_id" => $id));
            $db->delete("tbworkflow_lines", array("workflow_id" => $id));
            break;
        case "delete_print_report" :
            $db->delete("tb_generate", array("id" => $id));
            break;
    }
}

class UpdateStatus extends Database {

    // for workflow

    public function getFormID($id) {
        $getFormID = $this->query("SELECT form_id FROM tbworkflow WHERE id = $id", "row");
        return $getFormID['form_id'];
    }

    public function deactivateWorkflow($id) {
        $form_id = $this->getFormID($id);
        $set = array("Is_active" => "0");
        $where = array("form_id" => $form_id);
        $this->update("tbworkflow", $set, $where);
        return;
    }

    public function activateWorkflow($id) {
        $set = array("Is_active" => "1");
        $where = array("id" => $id);
        $this->update("tbworkflow", $set, $where);
        return;
    }

    //for form
    public function activateForm($id) {
        $set = array("Is_active" => "1");
        $where = array("id" => $id);
        $this->update("tb_workspace", $set, $where);
        return;
    }

    public function deactivateForm($id) {
        $set = array("Is_active" => "0");
        $where = array("id" => $id);
        $this->update("tb_workspace", $set, $where);
        return;
    }

    //for orgchart
    public function getOrgchartObjects($id) {
        $getOrgchartObjects = $this->query("SELECT * FROM tborgchartobjects WHERE orgChart_id = $id", "array");
        return $getOrgchartObjects;
    }

    public function updateUsersDepartment($id) {
        $department_json = $this->getOrgchartObjects($id);
        //remove department
        $this->removeUsersDepatment();


        // $department_json_head = $department_json['orgchart_user_head'];
        foreach ($department_json as $key => $value) {
            //head
            $json = json_decode($value['json']);
            $department_id = $value['id'];
            $json_head = $json->{'orgchart_user_head'};
            $json_assistant_head = $json->{'orgchart_dept_assistant'};
            $json_members = $json->{'orgchart_dept_members'};
            $users_arr = array_merge($json_head, $json_assistant_head, $json_members);
            foreach ($users_arr as $users_id) {
                $set = array("department_id" => $department_id);
                $where = array("id" => $users_id);
                $this->update("tbuser", $set, $where);
            }
        }
    }

    public function removeUsersDepatment() {
        $auth = Auth::getAuth('current_user');

        $company_id = $auth['company_id'];
        $set = array("department_id" => "0");
        $where = array("company_id" => $company_id);

        $this->update("tbuser", $set, $where);
    }

    public function activateOrgchart($id) {
        //update users
        $this->updateUsersDepartment($id);

        //activate orgchart
        $set = array("status" => "1");
        $where = array("id" => $id);

        $this->update("tborgchart", $set, $where);
    }

    public function deactivateOrgchart() {
        $auth = Auth::getAuth('current_user');

        $company_id = $auth['company_id'];

        //deactivate orgchart
        $set = array("status" => "0");
        $where = array("company_id" => $company_id);

        $this->update("tborgchart", $set, $where);
    }

}

?>